package com.cws4cloud.cws.mall.cms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cws4cloud.cws.common.core.util.R;
import com.cws4cloud.cws.common.log.annotation.SysLog;
import com.cws4cloud.cws.mall.cms.entity.Classlist;
import com.cws4cloud.cws.mall.cms.service.ClasslistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 栏目管理
 *
 * @author zhuljf
 * @date 2021-04-20 17:06:53
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/classlist" )
@Api(value = "classlist", tags = "栏目管理")
public class ClasslistController {

    private final ClasslistService classlistService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param classlist 栏目管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin_classlist_get')" )
    public R getClasslistPage(Page page, Classlist classlist) {
        return R.ok(classlistService.page(page, Wrappers.query(classlist)));
    }


    /**
     * 通过id查询栏目管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_classlist_get')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(classlistService.getById(id));
    }

    /**
     * 新增栏目管理
     * @param classlist 栏目管理
     * @return R
     */
    @ApiOperation(value = "新增栏目管理", notes = "新增栏目管理")
    @SysLog("新增栏目管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_classlist_add')" )
    public R save(@RequestBody Classlist classlist) {
        return R.ok(classlistService.save(classlist));
    }

    /**
     * 修改栏目管理
     * @param classlist 栏目管理
     * @return R
     */
    @ApiOperation(value = "修改栏目管理", notes = "修改栏目管理")
    @SysLog("修改栏目管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_classlist_edit')" )
    public R updateById(@RequestBody Classlist classlist) {
        return R.ok(classlistService.updateById(classlist));
    }

    /**
     * 通过id删除栏目管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除栏目管理", notes = "通过id删除栏目管理")
    @SysLog("通过id删除栏目管理" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_classlist_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(classlistService.removeById(id));
    }

	/**
	 * 返回树形站点
	 * @param parentId 父节点ID
	 * @return 树形站点
	 */
	@GetMapping(value = "/tree")
	public R getTree(boolean lazy, Integer parentId, String name, Integer siteId) {
		return R.ok(classlistService.listClasslistTrees(lazy, parentId, name, siteId));
	}

}
